import { type WorkBook, read, utils } from 'xlsx'

export async function getWorkBookFromFile(file: File): Promise<WorkBook> {
  const buffer = await file.arrayBuffer()
  return read(buffer)
}

export function parseSheetToList<T = unknown>(workBook: WorkBook, includeSheets: string[] = []) {
  const list: T[] = []

  workBook.SheetNames.forEach((key) => {
    if (includeSheets.includes(key)) {
      const sheet = workBook.Sheets[key]
      list.push(...(utils.sheet_to_json(sheet) as T[]))
    }
  })

  return list
}
